package com.slunk.platforms;

import com.slunk.interfaces.STargetedObjectInterface;
import com.slunk.platforms.STouchHandlerEvents;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.math.Vector3;

public class STouchHandler implements InputProcessor {
	STouchHandlerEvents eventHandler;
	OrthographicCamera camera;
	Array<STargetedObjectInterface> targetedObjects;

	public STouchHandler(STouchHandlerEvents eventHandler,OrthographicCamera camera) {
		this.camera = camera;
		this.eventHandler = eventHandler;
		targetedObjects = new Array<STargetedObjectInterface>();
	}

	public void RegisterTargetedObject(STargetedObjectInterface targetedObject) {
		this.targetedObjects.add(targetedObject);
		this.targetedObjects.sort();
		this.targetedObjects.reverse();
	}
	
	@Override
	public boolean keyDown(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		for (STargetedObjectInterface object : targetedObjects) {
			Vector3 touchPos = new Vector3();
			touchPos.set(screenX, screenY, 0);
			camera.unproject(touchPos);
			if (object.containsTouch(touchPos.x, touchPos.y)) {
				if (object.touchDown(touchPos.x, touchPos.y)) {
					eventHandler.targetedObjectTouchDown(object, touchPos.x, touchPos.y);
					break;
				}
			}
		}
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		for (STargetedObjectInterface object : targetedObjects) {
			 Vector3 touchPos = new Vector3();
	         touchPos.set(screenX, screenY, 0);
	         camera.unproject(touchPos);
	         if (object.containsTouch(touchPos.x, touchPos.y)) {
				if (object.touchUp()) {
					eventHandler.targetedObjectTouchUp(object, touchPos.x, touchPos.y);
					break;
				}
			}
		}
		return false;
	}

	@Override
	public boolean touchDragged(int screenX, int screenY, int pointer) {
		for (STargetedObjectInterface object : targetedObjects) {
			 Vector3 touchPos = new Vector3();
	         touchPos.set(screenX, screenY, 0);
	         camera.unproject(touchPos);
	         if (object.containsTouch(touchPos.x, touchPos.y)) {
				if (object.touchDragged()) {
					eventHandler.targetedObjectTouchDragged(object, touchPos.x, touchPos.y);
					break;
				}
			}
		}
		return false;
	}

	@Override
	public boolean mouseMoved(int screenX, int screenY) {
		for (STargetedObjectInterface object : this.targetedObjects) {
			 Vector3 touchPos = new Vector3();
	         touchPos.set(screenX, screenY, 0);
	         camera.unproject(touchPos);
	         if (object.containsTouch(touchPos.x, touchPos.y)) {
				if (object.mouseMoved(touchPos.x, touchPos.y)) {
					eventHandler.targetedObjectMouseMoved(object, touchPos.x, touchPos.y);
					break;
				}
			}
		}
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return false;
	}
}
